<?php
namespace Admin\Model;

use Think\Model\RelationModel;

class UserModel extends RelationModel
{
    public $_validate = array(
        //array('account','/^[a-z]\w{3,}$/i','帐号格式错误',1),
        array('account', 'require', '账号必须', 1),
        array('nickname', 'require', '姓名必须', 1),
        //array('repassword','require','确认密码必须',1),
        //array('repassword','password','确认密码不一致',1,'confirm'),
        array('account', '', '帐号已经存在！', 1, 'unique', 1),
        array('nickname', '', '姓名已经存在！', 1, 'unique', 1),
    );
    public $_auto = array(
        array('password', 'pwdHash', 1, 'callback'),
        //array('create_time','getDate',1,'function'),
        //array('update_time','getDate',2,'function'),
    );

    protected function pwdHash()
    {
        if (isset($_POST['password'])) {
            return md5($_POST['password']);
        } else {
            return md5(123456);
        }
    }
    protected $_link = array(
        'roles' => array(
            'mapping_type' => self::MANY_TO_MANY,
            'class_name' => 'role',
            'foreign_key' => 'uid',
            'relation_foreign_key' => 'group_id',
            'relation_table' => 'role_user',
            'mapping_order' => 'group_id', //排序
            //'as_fields'=>'name',
        ),
        'dept' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'dept', //对应的表名
            'foreign_key' => 'deptid', //外键名称
            'mapping_name' => 'dept', //查询完的集合名称
        ),
    );
}
